package com.edward.action;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.JsonTimeProcessor;
import com.edward.PagerModel;
import com.edward.model.LocalTax;
import com.edward.service.LocalTaxManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LocalTaxAction extends ActionSupport{


	private static final long serialVersionUID = 6967564864734915097L;

	private LocalTax localTax;
	
	private LocalTaxManager localTaxManager;
	
	private String query="";
	
	private int limit;
	
	private int start;
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	
	public LocalTax getLocalTax() {
		return localTax;
	}

	public void setLocalTax(LocalTax localTax) {
		this.localTax = localTax;
	}

	public void setLocalTaxManager(LocalTaxManager localTaxManager) {
		this.localTaxManager = localTaxManager;
	}

	@Secured("ROLE_COREFINANCE_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		LocalTax l = localTax;
		localTax.setSum(l.getCct() + l.getPet() + l.getSt() + l.getCet()
				+ l.getVat());
		localTaxManager.addLocalTax(localTax);
		localTaxManager.addcheck(localTax);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		LocalTax lt = localTax;
		localTax.setSum(lt.getCct() + lt.getPet() + lt.getSt() + lt.getCet()
				+ lt.getVat() );
		LocalTax ltax = localTaxManager.getLocalTax(localTax.getId());
		LocalTax l = localTaxManager.getSum(lt.getDate());
		l.setCct(l.getCct() + lt.getCct() - ltax.getCct());
		l.setPet(l.getPet() + lt.getPet() - ltax.getPet());
		l.setSt(l.getSt() + lt.getSt() - ltax.getSt());
		l.setCet(l.getCet() + lt.getCet() - ltax.getCet());
		l.setVat(l.getVat() + lt.getVat() - ltax.getVat());
		l.setSum(l.getSum()+lt.getSum() - ltax.getSum());
		localTaxManager.updateLocalTax(localTax);
		localTaxManager.updateLocalTax(l);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_COREFINANCE_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		LocalTax ltax = localTaxManager.getLocalTax(localTax.getId());
		LocalTax l = localTaxManager.getSum(ltax.getDate());
		l.setCct(l.getCct() - ltax.getCct());
		l.setPet(l.getPet() - ltax.getPet());
		l.setSt(l.getSt() - ltax.getSt());
		l.setCet(l.getCet() - ltax.getCet());
		l.setVat(l.getVat() - ltax.getVat());
		l.setSum(l.getSum() - ltax.getSum());
		localTaxManager.updateLocalTax(l);
		localTaxManager.deleteLocalTax(localTax.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	@Secured("ROLE_COREFINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonTimeProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonTimeProcessor());
		PagerModel pager;
	    pager = localTaxManager.pagerList(start, limit,query);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	public String getSumList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = localTaxManager.pagerSumList(start, limit,query);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_COREFINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	
	
	
	
	

}
